/**
 * Title：StringUtil.java
 * Description：msb-common
 * Copyright：Copyright (c)2008 Sobey,Inc
 * Company：Sobey,Inc
 * @author wangwei02 2008-3-20 上午10:13:42
 * @version 2.0.0
 */
package com.cnoja.jmsncn.utils.text;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 
 * 
 * Util of String operation
 * 
 */
public class StringUtils {

	/**
	 * Default Constructor
	 * 
	 */
	public StringUtils() {
	}

	/**
	 * Convert Unicode to GBK
	 * 
	 * @param str
	 *            the string needed to convert
	 * @return
	 * @throws Exception
	 */
	public static String unicodeToChinese(String str) throws Exception {
		if (str != null) {
			return new String(str.getBytes("ISO-8859-1"), "GBK");
		} else {
			return null;
		}
	}

	/**
	 * Juging the string is null or empty
	 * 
	 * @param stringToTest
	 * @return
	 */
	public static boolean isNullOrEmpty(String stringToTest) {
		return (stringToTest == null || stringToTest.trim().equals(""));
	}
	/**
	 * Juging the string is null or empty
	 * 
	 * @param stringToTest
	 * @return
	 */
	public static boolean notNullOrEmpty(String stringToTest) {
		return !(stringToTest == null || stringToTest.trim().equals(""));
	}

	/**
	 * Split the string to string[] according to the specified separator
	 * 
	 * @param string
	 *            the String needed to convert
	 * @param separator
	 *            the separator
	 * @return
	 */
	public static String[] split(String string, String separator) {
		if (isNullOrEmpty(string)) {
			return null;
		}
		if (isNullOrEmpty(separator)) {
			return new String[] { string };
		}
		return string.split(separator);
	}

	

	/**
	 * Put in 2 parameters ,the first is the String for convert ,if any
	 * Exceptions occurs,the default value will be returned.
	 * 
	 * @param stringToConvert
	 * @param defaultValOnError
	 * @return
	 */
	public static int StringToInt(String stringToConvert, int defaultValOnError) {
		if (!isNullOrEmpty(stringToConvert)) {
			try {
				return Integer.parseInt(stringToConvert);
			} catch (Exception e) {
				// The should be the normal circumstance ,do nothing.
			}
		}
		return defaultValOnError;
	}

	public static String list2String(List list) {
		String returnString = "";
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				String s = (String) list.get(i);
				returnString += "," + s;
			}
			returnString += ",";
		}
		return returnString;
	}

	public static String xmlEncode(String str) {
		if (str != null && str.trim().length() > 0) {
			str = str.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
					.replaceAll(">", "&gt;").replaceAll("'", "&apos;")
					.replaceAll("\"", "&quot;");
		}
		return str;
	}

	/**
	 * Escaping the str parameter,replace the symbols like "<",">"...
	 * 
	 * @param str
	 * @return
	 */
	public static String escapeXML(String str) {
		if (str != null && str.trim().length() > 0) {
			str = str.replaceAll("&lt;", "<").replaceAll("&gt;", ">")
					.replaceAll("&apos;", "'").replaceAll("&quot;", "\"")
					.replaceAll("&amp;", "&");
		}
		return str;
	}

	/**
	 * Parse a String object into the Date object.
	 * 
	 * @param DateString
	 * @return
	 */
	public static Date stringToDate(String DateString) {
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyy-MM-dd");
			return simpleDateFormat.parse(DateString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Format a string object contains the date infomation in to a Date object.
	 * 
	 * @param DateString
	 * @return
	 */
	public static Date stringToTime(String DateString) {
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			return simpleDateFormat.parse(DateString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param str
	 * @return
	 */
	public static String chineseToUnicode(String str) {
		try {
			if (str != null) {
				return new String(str.getBytes("GBK"), "ISO-8859-1");
				// return new String(str.getBytes("GBK"), "UTF-8");
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * Read the file into a String object.
	 * 
	 * @param filePath
	 * @return
	 */
	public static String readFileAsString(String filePath) {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader(filePath);
		} catch (FileNotFoundException fnfe) {
			return "";
		}
		try {
			StringBuffer fileData = new StringBuffer(1000);
			BufferedReader reader = new BufferedReader(fileReader);
			char[] buf = new char[1024];
			int numRead = 0;
			while ((numRead = reader.read(buf)) != -1) {
				String readData = String.valueOf(buf, 0, numRead);
				fileData.append(readData);
				buf = new char[1024];
			}
			reader.close();
			return fileData.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	

	

	

	

	public static String byte2Hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
		}
		return hs.toUpperCase();
	}

	

	public static String getTodayStr() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date());
	}

	

	public static boolean equalsStringName(String str, String ele) {
		return str.equals(ele.toLowerCase());
	}

	public static String inputStreamToString(InputStream inputStream,
			String charSetName) throws IOException {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		int b = -1;

		while ((b = inputStream.read()) != -1) {
			outputStream.write(b);
		}
		String s = outputStream.toString(charSetName);

		return s;
	}

	public static boolean anyNullOrEmpty(String... params) {
		if (params.length > 0) {
			for (String s : params) {
				if (isNullOrEmpty(s)) {
					return true;
				}
			}
		}
		return false;
	}

	public static String defaultIfNullOrEmpty(String defaultString,
			String forTest) {
		return isNullOrEmpty(forTest) ? defaultString : forTest;
	}
}
